﻿package com.eshore.player;

import android.content.Context;
import android.util.Log;


public class ImagePlayerViewRtmp extends ImagePlayerView {

	protected PlayerMainThread playerMainThread = null;
	protected PlayerIsReadyThread playerIsReadyThread = null;
	protected PlayerDrawImageThread playerDrawImageThread = null;
	protected String rtmpUrl = "";

	public ImagePlayerViewRtmp(Context context, int screenMode,
			int screenWidth, int screenHeight, String rtmpUrl) {
		super(context, screenMode, screenWidth, screenHeight);
		this.rtmpUrl = rtmpUrl;
		this.init();
	}

	public PlayerMainThread getPlayerMainThread() {
		return playerMainThread;
	}

	public PlayerIsReadyThread getPlayerIsReadyThread() {
		return playerIsReadyThread;
	}

	public PlayerDrawImageThread getPlayerDrawImageThread() {
		return playerDrawImageThread;
	}

	public String getRtmpUrl() {
		return rtmpUrl;
	}

	public void setRtmpUrl(String rtmpUrl) {
		this.rtmpUrl = rtmpUrl;
	}

	protected void startPlayerIsReadyThread() {
		if (playerIsReadyThread.isAlive() == false) {
			Log.d("ImagePlayer", "ffplay playerIsReadyThread start");
			playerIsReadyThread.start();
		}
	}

	protected void startPlayDrawImageThread() {
		if (playerDrawImageThread.isAlive() == false) {
			Log.d("ImagePlayer", "ffplay playerDrawImageThread start");
			playerDrawImageThread.start();
			// if(this.player != null) {
			// this.player.startTimerThread(); // 显示PTZ等的控制和取消等待框
			// }
		}
	}

	protected boolean loadLibSuccess = false;

	public boolean getLoadLibSuccess() {
		return this.loadLibSuccess;
	}

	@Override
	protected void init() {
		this.loadLibSuccess = initLib();
		// if(!this.loadLibSuccess) {
		// return ;
		// }
		if (playerMainThread == null) {
			playerMainThread = new PlayerMainThread(this);
		}
		if (playerIsReadyThread == null) {
			playerIsReadyThread = new PlayerIsReadyThread(this);
		}
		if (playerDrawImageThread == null) {
			playerDrawImageThread = new PlayerDrawImageThread(this);
		}
		Log.d("Test","OK5");
	}

	@Override
	public void start() {
		if (playerMainThread.isAlive() == false) {
			Log.d("ImagePlayer", "ffplay playerMainThread start");
			playerMainThread.start();
		}
		// createBitmap();
	}
	
	public void resume(){
		if (this.playerMainThread != null) {
//			if (this.playerMainThread.isAlive() == true) {
				Log.d("resume", "resume playerMainThread");
				this.playerMainThread.resume1();
//			}
		}
	}
	
	public void pause() {
		if (this.playerMainThread != null) {
//			if (this.playerMainThread.isAlive() == true) {
				Log.d("pause", "pause playerMainThread");
				this.playerMainThread.pause();
//			}
		}
	}
	@Override
	public void stop() {

		// if (this.loadLibSuccess) {
		//
		// try {
		// ImagePlayerView.ffplayStop();
		// } catch (Exception e) {
		//				
		// }
		// new Thread() {
		// public void run() {
		// int returnvalue = 0;
		// try {
		// returnvalue = ImagePlayerView.ffplayStop();
		// } catch (Exception e) {
		// returnvalue = 1;
		// }
		// if (returnvalue != 0) {
		// IshowEvList.d("ImagePlayer", "ffplay cannot stop");
		// }
		// }
		// }.start();
		// }
		if (this.playerMainThread != null) {
			if (this.playerMainThread.isAlive() == true) {
				Log.d("pause", "pause playerMainThread");
				this.playerMainThread.Stop();
				this.playerMainThread.interrupt();
			}
		}
		if (this.playerIsReadyThread != null) {
			if (this.playerIsReadyThread.isAlive() == true) {
				Log.d("pause", "pause playerIsReadyThread");
				this.playerIsReadyThread.pause();
				this.playerIsReadyThread.interrupt();
			}
		}
		if (this.playerDrawImageThread != null) {
			if (this.playerDrawImageThread.isAlive() == true) {
				Log.d("pause", "pause playerDrawImageThread");
				this.playerDrawImageThread.pause();
				this.playerDrawImageThread.interrupt();
			}
		}
		// try {
		// Thread.sleep(100);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		// IshowEvList.d("THREAD","sleep 100.");

		Log.d("stop", "closed playerMainThread");
		if (this.playerMainThread != null) {
			if (this.playerMainThread.isAlive() == true) {
				Log.d("stop", "pause playerMainThread");
				this.playerMainThread.interrupt();
				this.playerMainThread = null;
			}
		}
		Log.d("stop", "closed playerIsReadyThread");
		if (this.playerIsReadyThread != null) {
			if (this.playerIsReadyThread.isAlive() == true) {
				Log.d("stop", "pause playerIsReadyThread");
				this.playerIsReadyThread.interrupt();
				this.playerIsReadyThread = null;
			}
		}
		Log.d("stop", "closed playerDrawImageThread");
		if (this.playerDrawImageThread != null) {
			if (this.playerDrawImageThread.isAlive() == true) {
				Log.d("stop", "pause playerDrawImageThread");
				this.playerDrawImageThread.interrupt();
				this.playerDrawImageThread = null;
			}
		}
	}

}
